<?php
class classProductSearch extends phpajax{
    function input(){
		aread("FastSearchValue");
        aread("SearchValue");
        aread("ActualPage");
	}

	function loading() {}

    function main() {
        $_Input = new classInput();
        if($this->SearchValue=='') $this->SearchValue = $this->FastSearchValue;
        $this->SearchValue = $_Input->SterilizeForSearch($this->SearchValue);
        aprint('middle_container',self::getSearchForm());
    }

    private function getSearchForm(){
        $_Paging = new classPaging();
        $PagesList = $_Paging->showPageNumbersByAJAX($this->getPagingSQL(), $this->ActualPage);

        return '
            <table id="product_search_input_form">
                <tr>
                    <td style="text-align:right;">
                        <input type="hidden" id="Show_Product_Image" name="Show_Product_Image" value="">
                        <input type="hidden" id="Show_Product_ID" name="Show_Product_ID" value="">
                        <input type="hidden" id="Image_Show_As_Child" name="Image_Show_As_Child" value="">
                        <input type="hidden" id="Show_Product_Detail_By_ID" name="Show_Product_Detail_By_ID" value="">
                        <input type="hidden" name="ProductToBasket_ID" id="ProductToBasket_ID" value="">
                        <input type="hidden" name="ProductToBasket_Count" id="ProductToBasket_Count" value="">
                        <input type="hidden" name="ProductToBasket_Details" id="ProductToBasket_Details" value="">
                        <input type="hidden" name="ActualPage" id="ActualPage" value="'.$this->ActualPage.'">
                        <input type="text" name="SearchValue" id="SearchValue" value="'.$this->SearchValue.'">
                    </td>
                    <td style="text-align:left;">
                        <a 
                            href="'._URL_INACTIVE_ADDR_.'"
                            onclick="
                                document.getElementById(\'ActualPage\').value=\'0\';
                                classProductSearch();
                            "
                        >
                            '._i18n_searching_SEARCH_.'
                        </a>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        '.$PagesList.'
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        '.$this->getProductsList().'
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        '.$PagesList.'
                    </td>
                </tr>
            </table>
        ';
    }

    public function getFastSearchForm(){
        $_Skin = new classTemplate();
		$result = '';
        if(file_exists($_Skin->getActual().'/fast_search.inc.php'))
            include $_Skin->getActual().'/fast_search.inc.php';

		return $result;
    }

    private function getProductsList(){
        if(trim($this->SearchValue)=='') return '';
		$_Price = new classPriceExport();

		//table starts
		$result = '
            <br>
			<table class="css_product_mini_main_container">
				<tr>
		';
		//list of products
		$iNo = 0;

        //it shows product order by category
        $SQL_Result = mysql_query($this->getSearchSQL());

		$SQL_Result_Count = mysql_num_rows($SQL_Result);

        if($SQL_Result_Count<1) $result.='<td>'._i18n_searching_NO_RECORD_WAS_FOUND_.'</td>';

		while($sql_array_product = mysql_fetch_object($SQL_Result))
		{
			$iNo++;
			$result .= '
					<td>
			';

			//make image's form
			$product_image = $this->getImage($sql_array_product->ID);
			//make others data
			$product_short_name 	= $this->getLink($sql_array_product->ID, $sql_array_product->Product_Name, 'css_product_mini_name_link');
			$product_short_price 	= $_Price->simplePrice($sql_array_product->ID,1);
			$product_short_desc 	= $this->getLink($sql_array_product->ID, substr($sql_array_product->Product_Descr,0,_TEXTS_LENGHT_IN_PREVIEW_),'css_product_mini_desccription_link');

			//product form from template
			$_Skin = new classTemplate();
			include $_Skin->getActual().'/product_short.inc.php';

			$result .= '
					</td>
			';

			//__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__
			//treba testovat porovnavanie / obmedzit prazdne riadky a stlpce na konci / zatial vypada ze funguje...
			if(!($iNo % _PRODUCTS_COUNT_IN_SINGLE_LINE_) and ($iNo < $SQL_Result_Count))
			$result .= '
				</tr>
				<tr>
			';
			//!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!__!!!

		}
		//table ends
		$result .= '
				</tr>
			</table>
            <br>
            <br>
		';

		return $result;
	}

    private function getImage($imgID){
		if(
			file_exists(_PRODUCTS_IMAGE_DIR_.'/'.$imgID._IMAGES_FILE_TYPE_) and
			file_exists(_PRODUCTS_IMAGE_DIR_.'/'.$imgID._PREVIEW_IMAGE_SUFFIX_._IMAGES_FILE_TYPE_)
		){
            $_pService  = new classProductService;
            $asPInfo    = $_pService->getProductInfo($imgID);
            $_Follow    = new classFollow();
			$result = '
				<a href="'._URL_INACTIVE_ADDR_.'"
                    '.$_Follow->getJS(_i18n_product_ENLARGE_IMAGE_BY_CLICK_).'
                    onmousedown="
                        document.getElementById(\'Show_Product_Image\').value=\''._PRODUCTS_IMAGE_DIR_.'/'.$imgID._IMAGES_FILE_TYPE_.'\';
                        document.getElementById(\'Image_Show_As_Child\').value=\'0\';
                        document.getElementById(\'Show_Product_ID\').value=\''.$imgID.'\';
                    "
                    onmouseup="classImageViewer();"
				>
                    <img
                        class="css_product_detail_preview_image"
                        src="'._PRODUCTS_IMAGE_DIR_.'/'.$imgID._PREVIEW_IMAGE_SUFFIX_._IMAGES_FILE_TYPE_.'"
                        alt="'.$asPInfo['Product_Name'].'"
                    >
				</a>
			';
        }
		else
			$result ='';
		return $result;
	}

    private function getLink($iPID, $sPText, $sCSSDefinition){
        if(trim($iPID)=='' or !is_numeric($iPID) or $iPID<1) return '';

        return '
            <a
                href="'._URL_INACTIVE_ADDR_.'"
                onclick="
                    document.getElementById(\'Show_Product_Detail_By_ID\').value=\''.$iPID.'\';
                    classItemDetail();
                "
                class="'.$sCSSDefinition.'"
            >
                '.$sPText.'
            </a>
        ';
    }

    private function getSearchSQL(){
        if(trim($this->SearchValue)=='')
            return '';
        else{
            if(trim($this->ActualPage)=='' or !is_numeric($this->ActualPage))
                $this->ActualPage=0;
            return '
                SELECT ID, Product_Name, Product_Descr
                FROM products
                WHERE
                  MATCH (Product_Name) AGAINST (\''.$this->SearchValue.'*\' IN BOOLEAN MODE)
                  OR MATCH (Product_Code) AGAINST (\''.$this->SearchValue.'*\' IN BOOLEAN MODE)
                  OR MATCH (Product_Descr) AGAINST (\''.$this->SearchValue.'*\' IN BOOLEAN MODE)
                ORDER BY
                  10 * MATCH(Product_Code) AGAINST (\''.$this->SearchValue.'\')
                  + 5 * MATCH(Product_Name) AGAINST (\''.$this->SearchValue.'\')
                  + MATCH(Product_Descr) AGAINST (\''.$this->SearchValue.'\') DESC
                LIMIT '.$this->ActualPage*_ITEMS_COUNT_IN_SINGLE_PAGE_.', '._ITEMS_COUNT_IN_SINGLE_PAGE_;
        }
    }

    private function getPagingSQL(){
        if(trim($this->SearchValue)=='') 
            return '';
        else
            return '
                SELECT COUNT(*)
                FROM products
                WHERE
                  MATCH (Product_Name) AGAINST (\''.$this->SearchValue.'*\' IN BOOLEAN MODE)
                  OR MATCH (Product_Code) AGAINST (\''.$this->SearchValue.'*\' IN BOOLEAN MODE)
                  OR MATCH (Product_Descr) AGAINST (\''.$this->SearchValue.'*\' IN BOOLEAN MODE)
                ORDER BY
                  10 * MATCH(Product_Code) AGAINST (\''.$this->SearchValue.'\')
                  + 5 * MATCH(Product_Name) AGAINST (\''.$this->SearchValue.'\')
                  + MATCH(Product_Descr) AGAINST (\''.$this->SearchValue.'\') DESC
            ';
    }
}
?>
